<!-- 个人信息展示页面 (personalInformation.vue) -->
<template>
  <div class="min-h-screen bg-gray-50">
    <navigation></navigation>
    
    <div class="max-w-7xl mx-auto px-4 py-20">
      <!-- 顶部信息栏 -->
      <div class="flex flex-col md:flex-row justify-between items-start md:items-center mb-8 gap-6">
        <div class="flex items-center">
          <!-- 优化后的头像展示框 -->
          <div class="relative group">
            <a href="http://localhost:5173/modifiedInformation"> 
              <div class="relative w-24 h-24 md:w-32 md:h-32 rounded-full overflow-hidden border-4 border-white shadow-lg transform transition-all duration-300 group-hover:scale-105">
                <img 
                  :src="userData.userImg" 
                  class="w-full h-full object-cover"
                  alt="用户头像"
                >
                <div class="absolute inset-0 bg-gradient-to-t from-black/40 to-transparent opacity-0 group-hover:opacity-100 transition-opacity duration-300 flex items-end justify-center pb-3">
                  <span class="text-white text-sm bg-black/60 px-3 py-1 rounded-full">修改头像</span>
                </div>
              </div>
            </a>
            <!-- 新增：头像状态标签 -->
            <div class="absolute -bottom-2 -right-2 bg-green-500 text-white text-xs px-2 py-1 rounded-full shadow-lg">
              已认证
            </div>
          </div>
          
          <div class="ml-4">
            <h1 class="text-xl md:text-2xl font-bold text-gray-800 flex items-center">
              <span>{{ userData.name }}</span>
              <span class="ml-2 bg-blue-100 text-blue-800 text-xs px-2 py-0.5 rounded-full">{{ userData.position || '前端开发工程师' }}</span>
            </h1>
            <div class="flex items-center text-gray-600 mt-1">
              <el-icon class="text-gray-400 mr-1"><UserFilled /></el-icon>
              <span>{{ userData.age || '20' }}</span>
              <span class="mx-2 text-gray-300"></span>
            </div>
            <div class="flex items-center text-gray-600 mt-1">
              <el-icon class="text-gray-400 mr-1"><Message  /></el-icon>
              <span>{{ userData.email || '邮箱' }}</span>
            </div>
            <div class="flex items-center text-gray-600 mt-1">
              <el-icon class="text-gray-400 mr-1 text-lg"><Phone /></el-icon>
              <span>{{ userData.phone || '电话' }}</span>
            </div>
            <div class="flex items-center text-gray-600 mt-1">
              <el-icon class="text-gray-400 mr-1"><OfficeBuilding /></el-icon>
              <span>{{ userData.college || '计算机科学与技术学院' }}</span>
              <span class="mx-2 text-gray-300">|</span>
              <el-icon class="text-gray-400 mr-1"><Star /></el-icon>
              <span>{{ userData.education || '本科' }}</span>
              <el-icon class="text-gray-400 mr-1"><age /></el-icon>
            </div>
            <div class="flex items-center text-gray-600 mt-1">
              <el-icon class="text-gray-400 mr-1"><Calendar /></el-icon>
              <span>{{ userData.joinDate || '2023-09-01' }}</span>
            </div>
          </div>
        </div>
        
        <!-- 签到按钮 -->
        <div class="w-full md:w-auto">
          <button 
            @click="handleSignIn"
            class="flex items-center justify-center w-full md:w-auto bg-gradient-to-r from-blue-100 to-blue-50 px-6 py-3 rounded-full shadow-inner hover:from-blue-200 hover:to-blue-100 transition-all duration-300 cursor-pointer"
          >
            <el-icon class="text-blue-500 mr-2"><CircleCheck /></el-icon>
            <span class="text-blue-600 font-medium">签到</span>
            <span class="mx-3 text-gray-500">|</span>
            <span class="text-blue-800">已连续签到 {{ signInDays }} 天</span>
            <span v-if="!signedInToday" class="ml-3 bg-blue-600 text-white px-2.5 py-1 rounded-full text-xs shadow-md">+5 积分</span>
            <span v-else class="ml-3 bg-gray-300 text-gray-600 px-2.5 py-1 rounded-full text-xs">已签到</span>
          </button>
        </div>
      </div>

      <!-- 弹窗 -->
      <el-dialog
        v-model="signInDialogVisible"
        width="380px"
        :show-close="false"
        center
        class="signin-dialog"
      >
        <!-- 添加自定义header -->
        <template #header>
          <div class="dialog-header relative">
            <span class="text-gray-600 text-lg">每日签到</span>
            <button 
              @click="signInDialogVisible = false"
              class="close-btn absolute right-0 top-1/2 -translate-y-1/2 text-xl text-gray-400 hover:text-gray-600 transition-colors"
            >
              ×
            </button>
          </div>
        </template>

        <div class="text-center py-4">
          <!-- 实时日期（大字体） -->
          <p class="text-3xl font-medium text-gray-800 my-4">
            {{ currentDate }}
          </p>
          
          <!-- 连续签到天数 -->
          <p class="text-gray-500 text-base mb-6">
            已连续签到 <span class="text-blue-500 font-medium">{{ signInDays }}</span> 天
          </p>
          
          <!-- 立即签到按钮 -->
          <el-button 
            v-if="!signedInToday"
            type="primary" 
            size="large" 
            class="w-full mb-4"
            @click="confirmSignIn"
          >
            立即签到
          </el-button>
          <el-button 
            v-else
            type="info" 
            size="large" 
            class="w-full mb-4"
            disabled
          >
            今日已签到
          </el-button>
          
          <!-- 今日可获得积分 -->
          <div class="border border-gray-200 rounded-lg p-3 mb-6">
            <div class="flex items-center justify-center">
              <el-icon class="text-yellow-500 mr-1"><Money /></el-icon>
              <span class="text-gray-700">今日可获得 <span class="text-blue-500 font-medium">5</span> 积分</span>
            </div>
          </div>
          
          <!-- 底部链接 -->
          <div class="flex justify-between text-sm text-gray-500">
            <a href="#" class="hover:text-blue-500 " @click.prevent="showSignRecord">签到记录</a>
            <span>|</span>
            <a href="#" class="hover:text-blue-500" @click.prevent="goToPointsMall">积分商城</a>
          </div>
        </div>
      </el-dialog>

      <!-- 主要内容区域 - 调整为两栏布局 -->
      <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
        <!-- 左侧栏 -->
        <div class="space-y-6">
          <!-- 获奖证书 -->
          <div class="bg-white rounded-xl shadow-md overflow-hidden border border-gray-100">
            <div class="p-5 border-b border-gray-100 flex justify-between items-center">
              <div class="flex items-center">
                <el-icon class="text-blue-500 mr-2"><Trophy /></el-icon>
                <h2 class="text-lg font-semibold text-gray-800">获奖证书</h2>
              </div>
              <router-link 
                to="/certificateAuthority/competition" 
                class="text-blue-500 hover:text-blue-700 text-sm flex items-center"
              >
                查看更多 <el-icon class="w-4 h-4 ml-1"><ArrowRight /></el-icon>
              </router-link>
            </div>
            <div class="p-5 space-y-4">
              <div class="flex flex-col gap-1 p-3 hover:bg-gray-50 rounded-lg relative">
                <div class="flex justify-between items-center">
                  <h3 class="font-medium text-gray-800">前端开发工程师认证</h3>
                  <span class="text-green-500 text-sm flex items-center">
                    <el-icon class="mr-1"><CircleCheck /></el-icon> 已认证
                  </span>
                </div>
                <p class="text-sm text-gray-500">专业技能认证中心</p>
                <p class="text-xs text-gray-400 mt-1">2023-12-15</p>
              </div>
              
              <div class="flex flex-col gap-1 p-3 hover:bg-gray-50 rounded-lg relative">
                <div class="flex justify-between items-center">
                  <h3 class="font-medium text-gray-800">Web全栈开发证书</h3>
                  <span class="text-green-500 text-sm flex items-center">
                    <el-icon class="mr-1"><CircleCheck /></el-icon> 已认证
                  </span>
                </div>
                <p class="text-sm text-gray-500">互联网技术学院</p>
                <p class="text-xs text-gray-400 mt-1">2023-10-20</p>
              </div>
              
              <div class="flex flex-col gap-1 p-3 hover:bg-gray-50 rounded-lg relative">
                <div class="flex justify-between items-center">
                  <h3 class="font-medium text-gray-800">UI/UX设计认证</h3>
                  <span class="text-green-500 text-sm flex items-center">
                    <el-icon class="mr-1"><CircleCheck /></el-icon> 已认证
                  </span>
                </div>
                <p class="text-sm text-gray-500">设计师协会</p>
                <p class="text-xs text-gray-400 mt-1">2023-08-05</p>
              </div>
            </div>
          </div>

          <!-- 我的项目 -->
          <div class="bg-white rounded-xl shadow-md overflow-hidden border border-gray-100">
            <div class="p-5 border-b border-gray-100 flex justify-between items-center">
              <div class="flex items-center">
                <el-icon class="text-blue-500 mr-2"><Folder /></el-icon>
                <h2 class="text-lg font-semibold text-gray-800">我的项目</h2>
              </div>
              <router-link 
                 to="/certificateAuthority/project-experience"
                class="text-blue-500 hover:text-blue-700 text-sm flex items-center"
              >
                查看更多 <el-icon class="w-4 h-4 ml-1"><ArrowRight /></el-icon>
              </router-link>
            </div>
            <div class="p-5 space-y-4">
              <div class="border border-gray-100 rounded-lg p-4 hover:shadow-md transition-shadow">
                <h3 class="font-medium text-gray-800">在线教育平台</h3>
                <p class="text-sm text-gray-500 mt-2">基于Vue3和Spring Boot的在线教育平台</p>
                <div class="flex flex-wrap gap-2 mt-2">
                  <span class="bg-blue-100 text-blue-800 text-xs px-2 py-1 rounded">Vue3</span>
                  <span class="bg-blue-100 text-blue-800 text-xs px-2 py-1 rounded">Spring Boot</span>
                  <span class="bg-blue-100 text-blue-800 text-xs px-2 py-1 rounded">在线教育</span>
                </div>
                <p class="text-sm text-green-500 mt-2">项目成果：性能提升40%</p>
              </div>
              
              <div class="border border-gray-100 rounded-lg p-4 hover:shadow-md transition-shadow">
                <h3 class="font-medium text-gray-800">电商管理系统</h3>
                <p class="text-sm text-gray-500 mt-2">基于React和Node.js的电商后台管理系统</p>
                <div class="flex flex-wrap gap-2 mt-2">
                  <span class="bg-blue-100 text-blue-800 text-xs px-2 py-1 rounded">React</span>
                  <span class="bg-blue-100 text-blue-800 text-xs px-2 py-1 rounded">Node.js</span>
                  <span class="bg-blue-100 text-blue-800 text-xs px-2 py-1 rounded">电商系统</span>
                </div>
                <p class="text-sm text-green-500 mt-2">项目成果：服务超过100家企业</p>
              </div>
            </div>
          </div>
        </div>

        <!-- 右侧栏 -->
        <div class="space-y-6">
          <!-- 通用技能 -->
          <div class="bg-white rounded-xl shadow-md overflow-hidden border border-gray-100">
            <div class="p-5 border-b border-gray-100 flex justify-between items-center">
              <div class="flex items-center">
                <el-icon class="text-blue-500 mr-2"><Star /></el-icon>
                <h2 class="text-lg font-semibold text-gray-800">通用技能</h2>
              </div>
              <router-link 
                to="/certificateAuthority/skills" 
                class="text-blue-500 hover:text-blue-700 text-sm flex items-center"
              >
                查看更多 <el-icon class="w-4 h-4 ml-1"><ArrowRight /></el-icon>
              </router-link>
            </div>
            <div class="p-5 space-y-4">
              <div>
                <div class="flex justify-between items-center mb-1">
                  <h3 class="text-sm font-medium text-gray-700">前端开发</h3>
                  <span class="text-gray-500 text-sm">90%</span>
                </div>
                <el-progress :percentage="90" :show-text="false" :stroke-width="6" color="#3b82f6" />
              </div>
              <div>
                <div class="flex justify-between items-center mb-1">
                  <h3 class="text-sm font-medium text-gray-700">UI设计</h3>
                  <span class="text-gray-500 text-sm">80%</span>
                </div>
                <el-progress :percentage="80" :show-text="false" :stroke-width="6" color="#3b82f6" />
              </div>
              <div>
                <div class="flex justify-between items-center mb-1">
                  <h3 class="text-sm font-medium text-gray-700">项目管理</h3>
                  <span class="text-gray-500 text-sm">75%</span>
                </div>
                <el-progress :percentage="75" :show-text="false" :stroke-width="6" color="#3b82f6" />
              </div>
            </div>
          </div>

          <!-- 实习经历 -->
          <div class="bg-white rounded-xl shadow-md overflow-hidden border border-gray-100">
            <div class="p-5 border-b border-gray-100 flex justify-between items-center">
              <div class="flex items-center">
                <el-icon class="text-blue-500 mr-2"><Briefcase /></el-icon>
                <h2 class="text-lg font-semibold text-gray-800">实习经历</h2>
              </div>
              <router-link 
                to="/certificateAuthority/internship-experience" 
                class="text-blue-500 hover:text-blue-700 text-sm flex items-center"
              >
                查看更多 <el-icon class="w-4 h-4 ml-1"><ArrowRight /></el-icon>
              </router-link>
            </div>
            <div class="p-5 space-y-4">
              <div class="flex flex-col gap-1 p-3 hover:bg-gray-50 rounded-lg">
                <div class="flex items-start">
                  <div class="w-10 h-10 bg-blue-100 rounded-md flex items-center justify-center mr-3">
                    <el-icon class="text-blue-500"><OfficeBuilding /></el-icon>
                  </div>
                  <div>
                    <h3 class="font-medium text-gray-800">科技有限公司</h3>
                    <p class="text-sm text-gray-500">前端开发实习生</p>
                    <p class="text-xs text-gray-400">2023.06 - 2023.09</p>
                    <p class="text-sm text-gray-500 mt-1">参与企业级应用开发</p>
                  </div>
                </div>
              </div>
              
              <div class="flex flex-col gap-1 p-3 hover:bg-gray-50 rounded-lg">
                <div class="flex items-start">
                  <div class="w-10 h-10 bg-blue-100 rounded-md flex items-center justify-center mr-3">
                    <el-icon class="text-blue-500"><OfficeBuilding /></el-icon>
                  </div>
                  <div>
                    <h3 class="font-medium text-gray-800">互联网科技公司</h3>
                    <p class="text-sm text-gray-500">UI设计实习生</p>
                    <p class="text-xs text-gray-400">2023.02 - 2023.05</p>
                    <p class="text-sm text-gray-500 mt-1">负责产品界面设计</p>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <!-- 我的活动 -->
          <div class="bg-white rounded-xl shadow-md overflow-hidden border border-gray-100">
            <div class="p-5 border-b border-gray-100 flex justify-between items-center">
              <div class="flex items-center">
                <el-icon class="text-blue-500 mr-2"><Trophy /></el-icon>
                <h2 class="text-lg font-semibold text-gray-800">我的活动</h2>
              </div>
              <router-link 
                to="/certificateAuthority/activity" 
                class="text-blue-500 hover:text-blue-700 text-sm flex items-center"
              >
                查看更多 <el-icon class="w-4 h-4 ml-1"><ArrowRight /></el-icon>
              </router-link>
            </div>
            <div class="p-5">
              <div class="flex flex-col gap-1 p-3 hover:bg-gray-50 rounded-lg">
                <h3 class="font-medium text-gray-800">技术分享大会</h3>
                <div class="flex flex-wrap gap-2 mt-1 mb-2">
                  <span class="bg-blue-100 text-blue-800 text-xs px-2 py-1 rounded">演讲</span>
                  <span class="bg-blue-100 text-blue-800 text-xs px-2 py-1 rounded">前端技术</span>
                </div>
                <p class="text-sm text-gray-400">2023-12-20</p>
                <p class="text-sm text-gray-500 mt-1">演讲者</p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { 
  ArrowRight, 
  CircleCheck, 
  Calendar,
  Money,
  Trophy,
  Folder,
  Star,
  Briefcase,
  OfficeBuilding,
  UserFilled,
  Message
} from '@element-plus/icons-vue'
import navigation from "../../views/navigation.vue"
import { ref, computed, onMounted, watchEffect } from 'vue'
import { ElDialog, ElButton, ElIcon, ElProgress } from 'element-plus'

// 签到相关状态
const signedInToday = ref(false)
const signInDays = ref(0)
const signInDialogVisible = ref(false)
const signRecords = ref([])

// 用户信息 - 使用响应式对象
const userData = ref({
  name: '罗峰',
  age: 24,
  education: '本科',
  userImg: '/src/assets/default-avatar.png'
})

const currentDate = computed(() => {
  const now = new Date()
  return `${now.getMonth() + 1}月${now.getDate()}日`
})

// 格式化日期为 YYYY-MM-DD 格式
const formatDate = (date) => {
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  return `${year}-${month}-${day}`
}

// 获取今天的日期字符串
const getToday = () => {
  return formatDate(new Date())
}

// 获取昨天的日期字符串
const getYesterday = () => {
  const yesterday = new Date()
  yesterday.setDate(yesterday.getDate() - 1)
  return formatDate(yesterday)
}

// 获取当前用户的唯一标识
const getUserId = () => {
  // 从用户数据中获取唯一标识，如果没有则使用默认值
  return userData.value.id || 'default_user'
}

// 生成用户特定的存储键
const getUserStorageKey = (key) => {
  return `user_${getUserId()}_${key}`
}

// 保存签到记录到本地存储
const saveSignInData = () => {
  try {
    const signInData = {
      lastSignInDate: getToday(),
      signInDays: signInDays.value,
      signRecords: signRecords.value
    }
    localStorage.setItem(getUserStorageKey('signInData'), JSON.stringify(signInData))
  } catch (error) {
    console.error('保存签到数据失败:', error)
  }
}

// 从本地存储加载签到记录
const loadSignInData = () => {
  try {
    const savedData = localStorage.getItem(getUserStorageKey('signInData'))
    if (savedData) {
      const { lastSignInDate, signInDays: days, signRecords: records } = JSON.parse(savedData)
      
      // 设置签到状态
      signedInToday.value = lastSignInDate === getToday()
      signInDays.value = days || 0
      signRecords.value = records || []
      
      // 如果今天已经签到，确保记录中包含今天的记录
      if (signedInToday.value && !signRecords.value.some(record => record.date === getToday())) {
        signRecords.value.push({
          date: getToday(),
          points: 5
        })
      }
    }
  } catch (error) {
    console.error('加载签到数据失败:', error)
  }
}

// 处理签到逻辑
const handleSignIn = () => {
  if (!signedInToday.value) {
    signInDialogVisible.value = true
  }
}

// 确认签到
const confirmSignIn = () => {
  if (!signedInToday.value) {
    const today = getToday()
    const yesterday = getYesterday()
    
    // 检查是否连续签到
    const isContinuous = signRecords.value.some(record => record.date === yesterday)
    
    // 更新连续签到天数
    signInDays.value = isContinuous ? signInDays.value + 1 : 1
    
    // 添加新的签到记录
    signRecords.value.push({
      date: today,
      points: 5,
      isContinuous
    })
    
    // 更新签到状态
    signedInToday.value = true
    
    // 保存数据到本地存储
    saveSignInData()
    
    // 显示签到成功消息
    signInDialogVisible.value = true
  }
}

// 查看签到记录
const showSignRecord = () => {
  console.log('查看签到记录:', signRecords.value)
  // 这里可以添加打开签到记录页面的逻辑
}

// 前往积分商城
const goToPointsMall = () => {
  console.log('前往积分商城')
  // 这里可以添加前往积分商城的逻辑
}

// 从localStorage加载用户数据
const loadUserData = () => {
  try {
    const savedData = localStorage.getItem('userData')
    if (savedData) {
      userData.value = JSON.parse(savedData)
    }
  } catch (error) {
    console.error('加载用户数据失败:', error)
  }
}

// 监听用户数据变化，更新签到数据
const handleUserChange = () => {
  loadSignInData()
}

// 页面加载时初始化
onMounted(() => {
  loadUserData()
  loadSignInData()
  
  // 添加事件监听器监听localStorage变化
  window.addEventListener('storage', (event) => {
    if (event.key === 'userData') {
      loadUserData()
      handleUserChange()
    } else if (event.key === getUserStorageKey('signInData')) {
      loadSignInData()
    }
  })
  
  // 监听用户数据变化
  watchEffect(() => {
    // 当用户ID变化时，重新加载签到数据
    const userId = getUserId()
    handleUserChange()
  })
})

// 监听签到数据变化，确保数据一致性
watchEffect(() => {
  if (signRecords.value.length > 0) {
    saveSignInData()
  }
})
</script>

<style scoped>
/* 新增关闭按钮样式 */
.close-btn {
  background: transparent;
  border: none;
  outline: none;
  cursor: pointer;
  padding: 0 12px;
  transform: translateY(-50%);
}

/* 调整header区域样式 */
:deep(.el-dialog__header) {
  margin: 0;
  padding: 16px 20px;
  border-bottom: 1px solid #ebeef5;
}

.dialog-header {
  position: relative;
  padding-right: 30px; /* 给关闭按钮留出空间 */
}

/* 弹窗自定义样式 */
.signin-dialog {
  border-radius: 12px;
}

.signin-dialog :deep(.el-dialog__header) {
  padding-bottom: 0;
  margin-right: 0;
}

.signin-dialog :deep(.el-dialog__body) {
  padding: 0 20px;
}

/* 头像动画效果 */
.group:hover .group-hover\:scale-105 {
  transform: scale(1.05);
  transition: transform 0.3s ease;
}

.hover\:bg-gray-50:hover {
  transition: background-color 0.2s ease;
}

.hover\:shadow-md:hover {
  transition: box-shadow 0.3s ease;
}

.router-link-active {
  text-decoration: none;
}

.border-gray-100:hover {
  transform: translateY(-2px);
  transition: transform 0.3s ease;
}

.hover\:from-blue-200:hover {
  transition: background 0.3s ease;
}
.hover\:to-blue-100:hover {
  transition: background 0.3s ease;
}

/* 确保底部对齐 */
.grid > div {
  display: flex;
  flex-direction: column;
}

.grid > div > div:last-child {
  flex-grow: 1;
}

/* 新增样式：头像状态标签 */
.absolute-bottom-right {
  position: absolute;
  bottom: -5px;
  right: -5px;
}
</style>